package com.gg.widget

import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.widget.TextView
import android.widget.LinearLayout
import android.view.LayoutInflater
import android.view.View


/**
 * Creator : GG
 * Date    : 2018/3/27
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
internal class LoadingDialog : Dialog {
    private var parentLayout: LinearLayout? = null
    private var loadingText: TextView? = null

    constructor(context: Context?) : super(context, R.style.load_dialog) {
        init(context)
    }

    constructor(context: Context?, theme: Int) : super(context, R.style.load_dialog) {
        init(context)
    }

    private fun init(context: Context?) {
        this.setContentView(LayoutInflater.from(context).inflate(R.layout.load_dialog, null))
        parentLayout = findViewById(R.id.dialog_view) as LinearLayout
        loadingText = findViewById(R.id.text) as TextView
        this.setCancelable(false)
    }

    /**
     * 设置为空，方式内存泄漏
     */
    fun setTextViewNull() {
        loadingText = null
    }


    //设置名字和外层背景色
    fun setText(text: String, color: Int) {
        if (!TextUtils.isEmpty(text)) {
            parentLayout!!.setBackgroundColor(color)
            loadingText!!.text = text
            loadingText!!.visibility = View.VISIBLE
        } else {
            loadingText!!.visibility = View.GONE
        }
    }

    fun setText(text: String) {
        if (!TextUtils.isEmpty(text)) {
            loadingText!!.text = text
            loadingText!!.visibility = View.VISIBLE
        } else {
            loadingText!!.visibility = View.GONE
        }
    }
}